package com.goods.business.service.imp;

import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.system.Department;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(String pageNum, String pageSize) {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = ProductCategoryConverter.converterToProductCategoryTreeNodeVO(productCategoryList);
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOList);
        if (pageNum != null && pageSize!=null){
            List<ProductCategory> page = ListPageUtils.page(productCategoryList,Integer.parseInt(pageSize), Integer.parseInt(pageNum));
            return new PageVO<ProductCategoryTreeNodeVO>(page.size(),build);
        }
        return new PageVO<ProductCategoryTreeNodeVO>(0L,build);
    }
    //获取父级的分类信息
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = ProductCategoryConverter.converterToProductCategoryTreeNodeVO(productCategoryList);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList1 = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOList);
        return productCategoryTreeNodeVOList1;
    }
    //添加分类
    @Override
    public void add(ProductCategoryVO productCategoryVO) throws BusinessException {
        //判断之前是否有添加过的分类
        Example example = new Example(ProductCategory.class);
        example.createCriteria().andEqualTo("name",productCategoryVO.getName());
        int i = productCategoryMapper.selectCountByExample(example);
        if (i>0){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"添加的分类已存在!");
        }
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());

        productCategoryMapper.insert(productCategory);
    }
    //编辑分类
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = ProductCategoryConverter.converterToProductCategoryVO(productCategory);
        return productCategoryVO;
    }
    //修改分类
    @Override
    public void update(Long id,ProductCategoryVO productCategoryVO) throws BusinessException {
        //根据id查询 看是否存在分类的信息
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        if (productCategory == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"数据不存在!");
        }else {
            ProductCategory productCategory1 = new ProductCategory();
            BeanUtils.copyProperties(productCategoryVO,productCategory1);
            productCategory1.setId(id);
            productCategory1.setModifiedTime(new Date());
            productCategoryMapper.updateByPrimaryKey(productCategory1);
        }
    }
    //删除分类信息
    @Override
    public void delete(Long id) throws BusinessException {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        if(productCategory != null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"里面包含子分类信息或存在物资引用不能删除!");
        }
        productCategoryMapper.deleteByPrimaryKey(id);
    }

}
